#include "DesempaquetadorSoap.h"

DesempaquetadorSoap::DesempaquetadorSoap() {}

DesempaquetadorSoap::~DesempaquetadorSoap() {}

xmlDocPtr DesempaquetadorSoap:: Desempaquetar(char * rutaArchivo){

		std::string nameTagSoap, name;
		xmlDocPtr mensajeSoap, circuitoEnXml;
		xmlNodePtr rootSoap, nodeSoap, nodeXml, rootXml,nodeBody;
		if ((mensajeSoap = xmlParseFile(rutaArchivo)) == NULL) {

			return NULL;
		}

		circuitoEnXml = xmlNewDoc((xmlChar*)"1.0");
		rootXml = xmlNewDocNode(mensajeSoap, NULL,(xmlChar*)COMP ,NULL );
		xmlDocSetRootElement(circuitoEnXml, rootXml);

		rootSoap = xmlDocGetRootElement (mensajeSoap);
		nodeSoap = rootSoap->xmlChildrenNode;
		do{
			nameTagSoap = (char*)  nodeSoap->name;
			//tratamiento de Body
			if(nameTagSoap.compare(BODY)){
				nodeBody = nodeSoap->xmlChildrenNode;
				do{
					name = (char*)  nodeBody->name;
					if(name.compare(CIRCUITO) == 0){
						nodeXml = xmlNewChild(rootXml, NULL, (xmlChar*)nodeBody->name,NULL);
						this->parsear_circuito(nodeBody,nodeXml);
					}
					else if(name.compare(COMPUERTAS) == 0){
						nodeXml = xmlNewChild(rootXml, NULL, (xmlChar*)nodeBody->name,NULL);
						this->parsear_compuertas(nodeBody,nodeXml);
					}
					else if (name.compare(ENTRADAS) == 0){
						nodeXml = xmlNewChild(rootXml, NULL, (xmlChar*)nodeBody->name,NULL);
						this->parsear_entradas(nodeBody,nodeXml);
					}
					else if (name.compare(SALIDAS) == 0){
						nodeXml = xmlNewChild(rootXml, NULL, (xmlChar*)nodeBody->name,NULL);
						this->parsear_salidas(nodeBody,nodeXml);
					}
					else if (name.compare(CONEXIONES) == 0){
						nodeXml = xmlNewChild(rootXml, NULL, (xmlChar*)nodeBody->name,NULL);
						this->parsear_conexiones(nodeBody,nodeXml);
					}
					else if (name.compare(CABLES) == 0){
						nodeXml = xmlNewChild(rootXml, NULL, (xmlChar*)nodeBody->name,NULL);
						this->parsear_cables(nodeBody,nodeXml);
					}
					else if (name.compare(ELEMENTOS_EXTERNOS) == 0){
						nodeXml = xmlNewChild(rootXml, NULL, (xmlChar*)nodeBody->name,NULL);
						this->parsear_elementos_externos(nodeBody,nodeXml);
					}
					else{}

					nodeBody = nodeBody->next;
				}while(nodeBody);
			}
			nodeSoap = nodeSoap->next;
		}while(nodeSoap);

	return circuitoEnXml;
}

int DesempaquetadorSoap::obtenerEntradas(xmlDocPtr mensajeSoap){
	std::string nameTagSoap, name;
	char* entradas=NULL;
	xmlNodePtr rootSoap, nodeSoap,nodeBody;

	rootSoap = xmlDocGetRootElement (mensajeSoap);
	nodeSoap = rootSoap->xmlChildrenNode;

	do{
		nameTagSoap = (char*)  nodeSoap->name;
		//tratamiento de Body
		if(nameTagSoap.compare(BODY)){
			nodeBody = nodeSoap->xmlChildrenNode;
			do{
				name = (char*)  nodeBody->name;
				if(name.compare(ENTRADAS) == 0){
					entradas = (char*) xmlGetProp (nodeBody, (const xmlChar *)CANT_ENTR_SOAP);
					}
				nodeBody = nodeBody->next;
			}while(nodeBody);
		}
		nodeSoap = nodeSoap->next;
	}while(nodeSoap);

	return atoi(entradas);
}

int DesempaquetadorSoap::obtenerSalidas(xmlDocPtr mensajeSoap){
	std::string nameTagSoap, name;
	char* salidas=NULL;
	xmlNodePtr rootSoap, nodeSoap,nodeBody;

	rootSoap = xmlDocGetRootElement (mensajeSoap);
	nodeSoap = rootSoap->xmlChildrenNode;

	do{
		nameTagSoap = (char*)  nodeSoap->name;
		//tratamiento de Body
		if(nameTagSoap.compare(BODY)){
			nodeBody = nodeSoap->xmlChildrenNode;
			do{
				name = (char*)  nodeBody->name;
				if(name.compare(SALIDAS) == 0){
					salidas = (char*) xmlGetProp (nodeBody, (const xmlChar *)CANT_SAL_SOAP);
					}
				nodeBody = nodeBody->next;
			}while(nodeBody);
		}
		nodeSoap = nodeSoap->next;
	}while(nodeSoap);

	return atoi(salidas);
}

std::string DesempaquetadorSoap::obtener_simulacion(xmlDocPtr mensajeSoap){
	std::string nameTagSoap, name;
	char* simulacion=NULL;
	xmlNodePtr rootSoap, nodeSoap,nodeBody;

	rootSoap = xmlDocGetRootElement (mensajeSoap);
	nodeSoap = rootSoap->xmlChildrenNode;

	do{
		nameTagSoap = (char*)  nodeSoap->name;
		//tratamiento de Body
		if(nameTagSoap.compare(BODY)){
			nodeBody = nodeSoap->xmlChildrenNode;
			do{
				name = (char*)  nodeBody->name;

				if(name.compare(SIMULACION) == 0){
					simulacion = (char*) xmlGetProp (nodeBody, (const xmlChar *)RESULTADO);
					}
				nodeBody = nodeBody->next;
			}while(nodeBody);
		}
		nodeSoap = nodeSoap->next;
	}while(nodeSoap);

	return simulacion;
}

int DesempaquetadorSoap::obtener_orden_a_realizar(xmlDocPtr mensajeSoap, std::string& circuito, std::string& orden
												 ,std::string& entradas){
	std::string nameTagSoap;
	xmlNodePtr rootSoap, nodeSoap,nodeBody;

	rootSoap = xmlDocGetRootElement (mensajeSoap);
	nodeSoap = rootSoap->xmlChildrenNode;

	do{
		nameTagSoap = (char*)  nodeSoap->name;
		//tratamiento de Body
		if(nameTagSoap.compare(BODY)){
			nodeBody = nodeSoap->xmlChildrenNode;
			do{
				orden = (char*)  nodeBody->name;
				if(orden.compare(VER) == 0){
					circuito = (char*) xmlGetProp (nodeBody, (const xmlChar *)CIRCUITO_SOAP);
					return 1;
					}
				else if(orden.compare(DATA) == 0){
					circuito = (char*) xmlGetProp (nodeBody, (const xmlChar *)CIRCUITO_SOAP);
					return 1;
				}
				else if(orden.compare(SIMULAR) == 0){
					circuito = (char*) xmlGetProp (nodeBody, (const xmlChar *)CIRCUITO_SOAP);
					entradas = (char*) xmlGetProp (nodeBody, (const xmlChar *)ENTR_SOAP);
					return 1;
				}
				nodeBody = nodeBody->next;
			}while(nodeBody);
		}
		nodeSoap = nodeSoap->next;
	}while(nodeSoap);

	return 0;
}
